package org.example.day;

import org.example.linkedlist.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: Solution234
 * @Author wyatt
 * @Data 2024/06/27 21:27
 */
public class Solution234 {

    public static void main(String[] args) {
        Solution234 solution234 = new Solution234();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        System.out.println(solution234.isPalindrome(head));
    }

    public boolean isPalindrome(ListNode head) {
        ListNode tail = head;
        List<Integer> valList = new ArrayList<>();
        while (tail != null){
            valList.add(tail.val);
            tail = tail.next;
        }

        int left = 0;
        int right = valList.size()-1;
        while (left <= right){
            if(!valList.get(left).equals(valList.get(right))){
                return false;
            }
            left++;
            right--;
        }

        return true;
    }

    /**
     * @Description: 翻转链表后， 原链表顺序也被变更了
     *
     * @auther: wyatt
     * @date: 10:21 2024/6/28
    */
    @Deprecated
    public boolean isPalindrome2(ListNode head) {
        //先翻转链表，再逐个对比
        ListNode reversed = null;
        ListNode tail = head;
        while (tail != null){
            ListNode next = tail.next;
            tail.next = reversed;
            reversed = tail;
            tail = next;
        }

        ListNode p0 = head;
        ListNode p1 = reversed;
        while(p0 != null){
            if(p0.val != p1.val){
                return false;
            }
            p0 = p0.next;
            p1 = p1.next;
        }

        return true;
    }



}
